package com.base.myproject.client.busi.print;

import com.base.myproject.client.system.BaseResource;
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class PrintBcodeDialog implements ClickHandler,Listener<BaseEvent>{

	public Dialog d = new Dialog();
	com.extjs.gxt.ui.client.widget.FramePanel frame = com.extjs.gxt.ui.client.widget.FramePanel.pop();
	boolean autoprint=false;
	boolean autocancel = false;
	int autoprintmsecond=7;
	Html message = new Html("");
	String src = "";  //地址
	int width = 350;
	int higth = 300;
	
	public PrintBcodeDialog(String src){
		this.src = src;
		init();
	}
	
	public PrintBcodeDialog(String src, int width, int higth){
		this.src = src;
		this.width = width;
		this.higth = higth;
		init();
	}
	
	public void init(){
		initWidget();
	}
	
	public void initWidget(){
		frame.setId("__printingFrameX");
		frame.setBorders(false);
		
		d.setHeading("打印");
		d.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.print_16()));
		d.setLayout(new RowLayout(Orientation.HORIZONTAL));  
		d.add(frame);
		d.setSize(width, higth);

		d.setButtons(Dialog.YESNO);  
		d.getButtonById(Dialog.YES).setText("打印");
		d.getButtonById(Dialog.NO).setText("取消");
		d.getButtonById(Dialog.YES).setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.print_16()));
		
		d.getButtonById(Dialog.YES).addListener(Events.Select, this);
		d.getButtonById(Dialog.NO).addListener(Events.Select, this);
		if(autoprint)
			d.getButtonBar().add(message);
		
		d.addDomHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				autocancel=true;
			}} , ClickEvent.getType());
	}
	public com.extjs.gxt.ui.client.widget.FramePanel getFramePanel(){
		com.extjs.gxt.ui.client.widget.FramePanel frame=this.frame;
		return frame;
	}
	
	public void show(){
		
		d.show();
		d.setFocusWidget(d.getButtonById(Dialog.YES));
		frame.el().setSize(width, higth);
		load();
		if(autoprint)
		{
		 Timer t = new Timer() {
			 int i=0;
		      public void run() {
		    	  i++;
		    	  if(i>=autoprintmsecond){
		    		  cancel(); 
		    		  print();
		    		  message.setHtml("<a style=\"font-size: 14pt;\"><font color=\"#ff0000\">打印中</font> </a>");
		    	  }
		    	  if(autocancel){
		    		  cancel(); 
		    		  message.setHtml("");			    		
		    	  }
		    	  else{
		    		  message.setHtml("<a style=\"font-size: 14pt;\"><font color=\"#ff0000\">在"+(autoprintmsecond-i)+"秒后自动打印</font> </a>");
		    	  }
		      }
		    };
		    t.scheduleRepeating(1000);
		}
	}

	@Override
	public void onClick(ClickEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void handleEvent(BaseEvent be) {
		// TODO Auto-generated method stub
		if(be.getSource().equals(d.getButtonById(Dialog.NO))){
			d.hide();
		}
		else if(be.getSource().equals(d.getButtonById(Dialog.YES))){
			print();
		}
	}
	
	public void print()
	{
		
		 if (GXT.isIE) {
			 printFrame_IE();
		 }
		 else
		 {
			 printFrame();
		 }
	}

	//非ie下的打印
    static native void printFrame() /*-{
		var iframe = $doc.frames ? $doc.frames['__printingFrameX'] : $doc.getElementById('__printingFrameX');
	    var ifWin = iframe.contentWindow || iframe;
	    ifWin.print();
	}-*/;


    //ie下的frame打印
    static native void printFrame_IE()/*-{
    	$wnd.frames['__printingFrameX'].focus();
    	$wnd.frames['__printingFrameX'].print();
	}-*/;
	
	//载入打印模板
    public void load(){
		frame.el().setElementAttribute("src", src);
//		 com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
//			.create(com.base.myproject.client.tools.GreetingService.class);
//			greetingService.getDataSet("select bname as Cname,hr_firstname_en+hr_lastname_en as Ename,Identifier+bcode as " + 
//					"bcodeNum,departments as Cdepartment,enName as Edepartment,bcodeimagepath as image from(select bcode,bname,Identifier,hr_firstname_en,hr_lastname_en ,departments " +
//					"from bcode where bcode='"+bcode+"')as a,(select bcodeimagepath from bcode_images where bcode='"+bcode+"')as b,(select enName from Systeminfo where id=(select systemid from bcode " +
//					"where bcode='"+bcode+"')) as c", getAsyncCallback());
//	}
//	
//	private AsyncCallback<DataSet> getAsyncCallback(){
//		
//		return new AsyncCallback<DataSet>(){
//	
//			@Override
//			public void onFailure(Throwable caught) {
//				
//				MessageBox.alert("错误", "抱歉！网络传输发生错误。", null	);
//			}
//	
//			@Override
//			public void onSuccess(DataSet result) {
//				if(result.getRowCount()>0){
//					//随机数避免缓存
//					int randomint = Random.nextInt();
//					String cname = result.getValue(0,"Cname");
//					String ename = result.getValue(0,"Ename");
//					String bcodenum = result.getValue(0,"bcodeNum");
//					String cdepartment = result.getValue(0,"Cdepartment");
//					String edepartment = result.getValue(0, "Edepartment");
//					String imagepath = result.getValue(0,"image"); 
//					String params = "?cname="+ cname + "&ename=" + ename + "&bcodenum=" + bcodenum
//									+ "&cdepartment=" + cdepartment + "&edepartment=" + edepartment + "&imagepath=" + imagepath; 
//					if(imagepath.indexOf("?")>0)
//						imagepath +="&"+params+"&random="+randomint;
//					else
//						imagepath +="?"+params+"&random="+randomint;
//					frame.el().setElementAttribute("src","./bcodePrintImages.jsp"+params);
//				}
//				else{
//					MessageBox.alert("警告", "无提供打印格式", null);
//				}
//			}
//					
//		};
	}
}
